package com.chat.xx.server.impl;

import com.chat.xx.beam.entity.UserEntity;
import com.chat.xx.beam.vo.add.UserAdd;
import com.chat.xx.beam.vo.query.UserLoginVo;
import com.chat.xx.config.webSocket.MyWebSocketHandler;
import com.chat.xx.mapper.UserMapper;
import com.chat.xx.server.UserServer;
import com.chat.xx.beam.common.Message;
import com.chat.xx.utiles.SetMessage;
import com.chat.xx.utiles.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;
import java.util.List;
import java.util.Objects;

@Service
public class UserServerImpl implements UserServer {
    public final UserMapper userMapper;
    @Autowired
    public UserServerImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Message<UserEntity> add(UserAdd user) throws IllegalAccessException{
        if(Objects.isNull(user) || Tool.objProIsNull(user)){
            return SetMessage.set(201,"缺参");
        }
        WeekendSqls<UserEntity> custom = WeekendSqls.custom();
        custom.andEqualTo(UserEntity::getU_num,user.getU_num());
//        custom.andEqualTo(UserEntity::getU_name,user.getU_name()); 同时满足条件 才会被查出来
        List<UserEntity> list = userMapper.selectByExample(new Example.Builder(UserEntity.class).where(custom).build());
        if(list.isEmpty()){
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(user,userEntity);
            int sel =userMapper.insertSelective(userEntity);
            return sel == 0 ? SetMessage.set(201,"失败"):SetMessage.set(200,"成功",userEntity);
        }
        return SetMessage.set(202,"账号已存在");
    };

    @Override
    public Message<UserEntity> login(UserLoginVo user){
        WeekendSqls<UserEntity> custom = WeekendSqls.custom();
        custom.andEqualTo(UserEntity::getU_num,user.getU_num());
        custom.andEqualTo(UserEntity::getU_pwd,user.getU_pwd());
        List<UserEntity> list = userMapper.selectByExample(new Example.Builder(UserEntity.class).where(custom).build());
        if(list.isEmpty()){
            return SetMessage.set(201,"账户或密码错误!");
        }else {
            UserEntity userInfo = list.get(0);
            MyWebSocketHandler.sendAllMessage(Tool.wbSendMsg("LOGIN",userInfo));
            return SetMessage.set(200,"成功",userInfo);
        }
    }
    @Override
    public Message<Boolean> outLogin(Integer u_id){
        WeekendSqls<UserEntity> custom = WeekendSqls.custom();
        custom.andEqualTo(UserEntity::getU_id,u_id);
        List<UserEntity> list = userMapper.selectByExample(new Example.Builder(UserEntity.class).where(custom).build());
        if(!list.isEmpty()){
            UserEntity userInfo = list.get(0);
            MyWebSocketHandler.sendAllMessage(Tool.wbSendMsg("OUT",userInfo));
        }
        return SetMessage.set(200,"成功",Boolean.TRUE);
    }
    @Override
    public String getUserNameById(Integer u_id){
        WeekendSqls<UserEntity> custom = WeekendSqls.custom();
        custom.andEqualTo(UserEntity::getU_id,u_id);
        List<UserEntity> list = userMapper.selectByExample(new Example.Builder(UserEntity.class).where(custom).build());
        if(!list.isEmpty()){
            UserEntity userInfo = list.get(0);
            return userInfo.getU_name();
        }
        return "";
    }
}
